{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fairlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib widget\n",
    "\n",
    "import altair as alt\n",
    "from vega_datasets import data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load experimental results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Moji_results = fairlib.analysis.retrive_results(\"Moji\", log_dir=\"../analysis/results\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Moji_plot_df = fairlib.analysis.final_results_df(\n",
    "    # model_order=[\"Adv\",\"INLP\", \"DAdv\"],\n",
    "    results_dict = Moji_results,\n",
    "    pareto = True,\n",
    "    pareto_selection = \"test\",\n",
    "    selection_criterion = None,\n",
    "    return_dev = True,\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Crete Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c5ce323076f54824b4e2eca3a87bfdf7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from pathlib import Path\n",
    "def make_plot(plot_df, figure_name=None):\n",
    "    plot_df[\"Fairness\"] = plot_df[\"test_fairness mean\"]\n",
    "    plot_df[\"Accuracy\"] = plot_df[\"test_performance mean\"]\n",
    "\n",
    "    figure = plt.figure(dpi = 100) \n",
    "    with sns.axes_style(\"white\"):\n",
    "        sns.lineplot(\n",
    "            data=plot_df,\n",
    "            x=\"Accuracy\",\n",
    "            y=\"Fairness\",\n",
    "            hue=\"Models\",\n",
    "            markers=True,\n",
    "            style=\"Models\",\n",
    "        )\n",
    "\n",
    "    plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))\n",
    "    plt.tight_layout()\n",
    "\n",
    "    if figure_name is not None:\n",
    "        figure.savefig(Path(r\"plots\") / figure_name, dpi=960, bbox_inches=\"tight\") \n",
    "\n",
    "\n",
    "make_plot(Moji_plot_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1e774540895a419493bebe3fd75b1a67",
       "version_major": 2,
       "version_minor": 0
      },
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='' width=1200.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fairlib.analysis.tables_and_figures.interactive_plot(Moji_plot_df, selection=\"DTO\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2488cb460d0b49fc923ded756ef6bc44",
       "version_major": 2,
       "version_minor": 0
      },
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='' width=1200.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fairlib.analysis.tables_and_figures.interactive_plot(Moji_plot_df, selection=\"constrained\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "086b6e2ac5931a4f7a51b812f0475290a145e7d51f3e22b53c52adf5c273fe30"
  },
  "kernelspec": {
   "display_name": "Python 3.7.12 ('py37')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
